﻿using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.ServiceModel;
using System.Threading;

namespace ConfExpert.Forms.SatinAlma
{
    static class Unsped_Util
    {
        public static string Rfrsh(int Donem)
        {
            string xml = string.Empty;
            DateTime t = Session.DataManager.ExecuteScalar("GetUNSPED_MaxDate2").ToDate();
            if (t.Year < 2015)
                return "Webservisi son 1 dk. içinde çağrıldığından verilerin tekrar çekilmesine gerek duyulmadı.";
            else
            {
                int DurumId = 3;    //hata
                int LogUNSPEDId = 0;

                try
                {
                    while (true)
                    {
                        LogUNSPEDId = Session.DataManager.ExecuteScalar("CheckUNSPED", Session.CurrentUser.UserId).ToInt();
                        if (LogUNSPEDId > 0)
                            break;

                        Thread.Sleep(2000);
                    }
                    
                    UnspedWS.KarbelDataWsdlSoapClient unsped = new UnspedWS.KarbelDataWsdlSoapClient();
                    string userName = AppSession.UnspedUsername;
                    string password = AppSession.UnspedPassword;
                    BasicHttpBinding binding = (BasicHttpBinding)unsped.Endpoint.Binding;
                    binding.MaxBufferSize = Int32.MaxValue;
                    binding.MaxReceivedMessageSize = Int32.MaxValue;
                    binding.SendTimeout = new TimeSpan(1, 0, 0);
                    xml = unsped.KarbelVerileriniAl(userName, password, t.ToStr(), DateTime.Today.AddDays(1).ToStr(), "T");
                    
                    //xml = Session.DataManager.ExecuteScalar("GetUNSPEDXml").ToStr();

                    if (xml != "Belirtmiş olduğunuz tarih aralığında veri bulunamadı !!!")
                        if (xml != "Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.")
                        {
                            System.IO.StringReader xmlSR = new System.IO.StringReader(xml);
                            System.Data.DataSet dsKarbel = new DataSet();
                            dsKarbel.ReadXml(xmlSR);

                            Session.DataManager.ExecuteNonQuery("Truncate_Unsped2", xml);

                            foreach (DataTable dt in dsKarbel.Tables)
                                if (dt.TableName.EndsWith("_ROW"))
                                {
                                    foreach (DataRow r in dt.Rows)
                                        foreach (DataColumn c in dt.Columns)
                                            if (r[c.ColumnName].ToStr() == string.Empty)
                                                r[c.ColumnName] = null;

                                    dt.BulkCopy(Session.DataManager.ConnectionString, "UNSPED_" + dt.TableName);
                                }

                            Session.DataManager.ExecuteScalar("MergeUNSPED4", Donem).ToInt();
                            DurumId = 2;    //success
                            return dsKarbel.Tables[0].Rows.Count + " DOSYANIN BİLGİSİ GÜNCELLENDİ.";

                        }
                }

                catch (Exception Ex)
                {
                    if (xml.Length < 1000)
                        XtraMessageBox.Show(xml);
                    else
                        XtraMessageBox.Show(Ex.Message);
                }
                finally
                {
                    Session.DataManager.ExecuteScalar("BitirUNSPED", LogUNSPEDId, DurumId).ToInt();
                }
            }


            return string.Empty;

        }
    }
}